<script setup>
import {
    User,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { ref } from 'vue'
const tableData = ref()
import { allClassInfoService } from '@/api/allClassInfo.js'
const getClassInfo = async () => {
    let result = await allClassInfoService();
    //console.log(result)
    tableData.value = result.data;
}
getClassInfo();
import { studentInfoService,changePassword } from '@/api/studentInfo.js'
const studentName = ref()
const getStudentInfo = async () => {
    let result = await studentInfoService();
    //console.log(result)
    studentName.value = result.data.name;
}
getStudentInfo();
import { useRouter } from 'vue-router';
const router = useRouter()
import { useClassIDStore } from '@/stores/classID.js'
const classIDstore = useClassIDStore();
const conversation = async (rowClass) => {
    let classID = rowClass.classID
    classIDstore.setClassID(classID)
    console.log(classID)
    router.push('/studentChat');
}
import { ElMessage, ElMessageBox } from 'element-plus'
import { useTokenStore } from '@/stores/token.js'
const tokenStore = useTokenStore();
const logout = () => {
    //退出登录
    ElMessageBox.confirm(
        '你确认退出登录码？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //用户点击了确认
            //清空pinia中的token和个人信息
            tokenStore.token = ''
            //跳转到登录页
            router.push('/')
        })
        .catch(() => {
            //用户点击了取消
            ElMessage({
                type: 'info',
                message: '取消退出',
            })
        })
}
const dialogFormVisible=ref(false)
const form =ref({
  password:''
})
const changePass = () => {
  dialogFormVisible.value=true
};
const saveEdit=async()=>{
  const data={
    password:form.value.password
  }
  await changePassword(data)
  .then((res)=>{
  if(res.code===305){
  ElMessage.success('修改成功！')
  }else{
  ElMessage.error('修改出錯！')
  }
})
  dialogFormVisible.value=false
  
}
</script>
<template>
    <el-container class="layout-container">
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>学生：<strong>{{ studentName }}</strong></div>
                <el-dialog title="学生修改密码" v-model="dialogFormVisible" :before-close="handleClose">
                    <el-form :model="form">
                        <el-form-item label="新密码" :label-width="formLabelWidth">
                            <el-input v-model="form.password" autocomplete="off" />
                        </el-form-item>
                    </el-form>
                    <template #footer>
                        <span class="dialog-footer" @click="dialogFormVisible = false">
                            <el-button>取消</el-button>
                            <el-button type="primary" @click="saveEdit">
                                确认
                            </el-button>
                        </span>
                    </template>
                </el-dialog>
                <el-dropdown placement="bottom-end">
                    <span class="el-dropdown__box">
                        <el-avatar :src="avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="logout" :icon="SwitchButton"
                                @click="changePass()">修改密码</el-dropdown-item>
                        </el-dropdown-menu>
                        <el-dropdown-menu>
                            <el-dropdown-item command="logout" :icon="SwitchButton"
                                @click="logout()">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main class="card-container">
                <el-card class="custom-card" v-for="(item, index) in tableData" :key="index"
                    :style="{ maxWidth: '400px', minWidth: '400px' }">
                    <template #header>
                        <div class="card-header">
                            <span>{{ item.className }}</span>
                        </div>
                    </template>
                    <div>课程：{{ item.courseName }}</div>
                    <div>授课教师：{{ item.name }}</div>
                    <div>开课时间：{{ item.startTime }}</div>
                    <el-button @click="conversation(item)">进入</el-button>
                </el-card>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>问答通</el-footer>
        </el-container>
    </el-container>
</template>
<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }

    .card-container {
        display: flex;
        flex-wrap: wrap;
        /* 允许子元素在需要时换行 */
        justify-content: flex-start;
        /* 子元素从容器起始位置开始排列 */
        gap: 40px;

        /* 调整间隔大小 */
        .custom-card {
            border-radius: 15px;
            /* 圆润的边角 */
            overflow: hidden;
            /* 防止内容溢出边框 */
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            /* 添加阴影效果 */
            width: 400px;
            height: 200px;
            /* 调整卡片宽度 */
        }
    }


}
</style>